﻿/* GLOBAL VARIABLES */
var totalGamer = 4;
var round=0; //Vong danh

var colorB = '#000' ;
var colorR = '#D40000';
var srcClub = 'image/c.png';
var srcDiamond = 'image/d.png';
var srcHeart = 'image/h.png';
var srcSpade = 'image/s.png';
var srcBackCard = 'image/matsau.png';

var arrayNum = new Array();
arrayNum[0] = [0,null];
arrayNum[1] = [1,"A"];
arrayNum[2] = [2,"2"];
arrayNum[3] = [3,"3"];
arrayNum[4] = [4,"4"];
arrayNum[5] = [5,"5"];
arrayNum[6] = [6,"6"];
arrayNum[7] = [7,"7"];
arrayNum[8] = [8,"8"];
arrayNum[9] = [9,"9"];
arrayNum[10] = [10,"10"];
arrayNum[11] = [11,"J"];
arrayNum[12] = [12,"Q"];
arrayNum[13] = [13,"K"];

var arraySuit = new Array();
arraySuit[0] = [0,srcBackCard,null];
arraySuit[1] = [1,srcSpade,colorB];
arraySuit[2] = [2,srcClub,colorB];
arraySuit[3] = [3,srcDiamond,colorR];
arraySuit[4] = [4,srcHeart,colorR];

var arrGamer = new Array();
function objGamer(_id, _name, _ag, _ip){
	this.id = _id;
	this.name = _name;
	this.ag = _ag;
	this.ip = _ip;
	this.realPos = _realPos;
	this.virtualPos = 0;
	return true;
}
function initArrGamer(_strInfo){
	
}

function objCard(_numId, _suitId){
	var j=null, k=null;
	for(var i=0;i<arrayNum.length;i++){
		if(_numId==arrayNum[i][0]){
			j = i;
			break;
		}
	}
	for(var i=0;i<arraySuit.length;i++){
		if(_suitId==arraySuit[i][0]){
			k = i;
			break;
		}
	}
	this.num = arrayNum[j];
	this.suit = arraySuit[k];
	this.option = 0; //1: click
	this.checkH = false; //check[true]: nam trong fom ngang
	this.checkV = false; //check[true]: nam trong fom doc	
	this.take = false; //take[true]: quan bai an
	return true;
}
var objCardMe = new Object;
function initCardMe(_type, _nName, _name, _arrNumId, _arrSuitId){
	this.type = _type;
	this.nname = _nName;
	this.name = _name;
	this.arrCard = [];
	for(var m=0;m<_arrNumId.length;m++){
		this.arrCard[m] = new objCard(_arrNumId[m], _arrSuitId[m]);
	}
	this.click = function(_id, _option){
		this.arrCard[_id].option = _option;
	};
	this.methodAdd = function(_objCard){
		this.arrCard.push(_objCard);
	};
	this.methodRemove = function(_index){
		this.arrCard.splice(_index,1);
	};
	// true[false] : sap xep theo so[chat]
	this.flagSort = null;
	this.sort = function(_flagSort){
		if(_flagSort == true){
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].num[0]) == parseInt(this.arrCard[i].num[0])){
						if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = false;
		}else{
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].suit[0]) == parseInt(this.arrCard[i].suit[0])){
						if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = true;
		}
	};
	//	dat quan bai nam trong fom ngang hoac doc(true:trong fom)
	this.setCheckH = function(_id, _flag) {
		this.arrCard[_id].checkH = _flag;
	};
	this.setCheckV = function(_id, _flag) {
		this.arrCard[_id].checkV = _flag;
	};
	//	khi an thi setTake quan bai an = true
	this.setTake = function(_take) {
		this.arrCard[_take].take = true;
	};
	this.findTake = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				count++;
			}
		}
		return count;
	};
	this.arrTake = function() {
		var arrT = new Array();
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				arrT.push(this.arrCard[i]);
			}
		}
		return arrT;
	};
	return true;
}

function objCardState(_state, _arrCard){//obj card nam trong fom
	this.state = _state;
	this.arrCard = [];
	for(var i=0;i<_arrCard.length;i++){
		this.arrCard.push(_arrCard[i]);
	}
	this.methodAdd = function(_objCard) {
		this.arrCard.push(_objCard);
	};
	this.methodRemove = function(_index) {
		this.arrCard.splice(_index,1);
	};
	this.findTake = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				count++;
			}
		}
		return count;
	};
	this.findH = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].checkH == true){
				count++;
			}
		}
		return count;
	};
	this.findV = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].checkV == true){
				count++;
			}
		}
		return count;
	};
	this.totalPoint = function(){
		var point = 0;
		for(var i=0;i<this.arrCard.length;i++){
			point = point + this.arrCard[i].num[0];
		}
		return point;
	};
	return true;
}


var arrCardGamble = new Array();
function cardGamble(_idGamer){
	this.idGamer = _idGamer;
	this.arrCard = [];
	this.methodAdd = function(_objCard){
		this.arrCard.push(_objCard);
	};
	this.methodRemove = function(_indexCard){
		this.arrCard.splice(_indexCard,1);
	};
	return true;
}
function initArrCardGamble(){
	// 0 la gamer1(Me)
	for(var i=0;i<totalGamer;i++){
		arrCardGamble.push(new cardGamble(arrGamer[i].id));
	}
}

var a4 = new cardGamble(3);
a4.methodAdd(new objCard(1,1));
arrCardGamble[0] = new cardGamble(0);
arrCardGamble[1] = new cardGamble(1);
arrCardGamble[2] = new cardGamble(2);
arrCardGamble[3] = a4;


/* FUNCTION PLAY GAME */
function start(strStart){
	// Lay du lieu tu server: 
	document.getElementById('btnStart').style.display='none';
	document.getElementById('cardAll').style.display='block';
	document.getElementById('card1').style.display='block';
	document.getElementById('card2').style.display='block';
	document.getElementById('card3').style.display='block';
	document.getElementById('card4').style.display='block';
	var ele = getElementsByClassName('gamble',document.getElementById('table'));
	for(var i=0;i<ele.length;i++){
		ele[i].style.display = 'block';
	}
	
	var arr = jsonStart(strStart);
	objCardMe = new initCardMe(arr[0], arr[1], arr[2], arr[3], arr[4]);
	objCardMe.setTake(1);
	objCardMe.setTake(3);
//	objCardMe.setTake(6);
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		var div = document.createElement('div');
		div.setAttribute('id','card1_'+j+'');
		div.setAttribute('class','card');
		div.setAttribute('onclick','clickCard(this)');
		div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
		document.getElementById("card1").appendChild(div);		
		var span = document.createElement('span');
		span.setAttribute('id','span'+j+'');
		span.setAttribute('class','spanNum');
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[i].suit[2];
		document.getElementById('card1_'+j+'').appendChild(span);
		var txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		document.getElementById('span'+j+'').appendChild(txt);	
		var left = 9.5*i;
		new Motion(div,[50,-225],[left,0],[0,0],[12.5,100],150+i*20);
		div.style.display = 'block';
		div.style.zIndex = ''+j+'';
	}	
	new Motion(document.getElementById("card2"),[50,50],[80,40],[0,0],[7,20],155);
	new Motion(document.getElementById("card3"),[50,50],[46,2],[0,0],[7,20],160);
	new Motion(document.getElementById("card4"),[50,50],[10.5,40],[0,0],[7,20],165);
	startTimer("timer1",1);
}

function sortCard(){
	if(objCardMe.flagSort == null) objCardMe.sort(false);
	else objCardMe.sort(objCardMe.flagSort);
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		removeElement('card1','card1_'+j+'');
		var _div = document.createElement('div');
		_div.setAttribute('id','card1_'+j+'');
		_div.setAttribute('class','card');
		_div.setAttribute('onclick','clickCard(this)');
		_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
		document.getElementById("card1").appendChild(_div);		
		var _span = document.createElement('span');
		_span.setAttribute('id','span'+j+'');
		_span.setAttribute('class','spanNum');
		_span.style.backgroundColor = '#fff';
		_span.style.color = objCardMe.arrCard[i].suit[2];
		document.getElementById('card1_'+j+'').appendChild(_span);
		var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		document.getElementById('span'+j+'').appendChild(_txt);
		new Motion(_div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],20+i*5);
		_div.style.display = 'block';
		_div.style.zIndex = ''+j+'';
	}
}

/******************** DANH BAI ***********************/
function clickCard(_obj){
	var _objid = _obj.id;
	var index = _objid.indexOf('_');
	var count = parseInt(_objid.substr(index+1,_objid.length))-1;
	if(objCardMe.arrCard[count].option == 0){
		_obj.style.top="-20%";
		objCardMe.click(count, 1);
		return;
	}else{
		_obj.style.top="0%";
		objCardMe.click(count, 0);
		return;
	}
}
// Function lay bai danh cua cac gamer # tu server va hien thi
function getGamble(_idGamer, _objCard){
	for(var i=0;i<arrCardGamble.length;i++){
		if(arrCardGamble[i].idGamer == _idGamer){
			var _id = _idGamer+1;
			arrCardGamble[i].methodAdd(_objCard);			
			var _round = round+1;
			
			var div = document.createElement('div');
			div.setAttribute('id','gamble'+_id+'_'+_round+'');
			div.setAttribute('class','card_gamble');
			div.style.backgroundImage='url('+arrCardGamble[i].arrCard[round].suit[1]+')';
			document.getElementById('gamer'+_id+'_gamble').appendChild(div);
			
			var span = document.createElement('span');
			span.setAttribute('id','spanGamble'+_id+'_'+_round+'');
			span.setAttribute('class','spanNum');
			document.getElementById('gamble'+_id+'_'+_round+'').appendChild(span);
			var txt = document.createTextNode(arrCardGamble[i].arrCard[round].num[1]);		
			document.getElementById('spanGamble'+_id+'_'+_round+'').appendChild(txt);		
			span.style.backgroundColor = '#fff';
			span.style.color = arrCardGamble[i].arrCard[round].suit[2];		
			if(_idGamer == 1){
				new Motion(div,[115,105],[20.5*round,0],[35,100],[35,100],15);
			}else if(_idGamer == 2){
				new Motion(div,[-70,125],[20.5*round,0],[35,100],[35,100],15);
			}else if(_idGamer ==3){
				new Motion(div,[-80,-53],[20.5*round,0],[35,100],[35,100],15);
			}
			div.style.display ='block';
			div.style.zIndex = ''+_round+'';			
			break;
		}
	}	
}

// Function đánh bài cua Me
function setGamble(){
	if(round>3) return;
	var count = 0;
	var index=null;	
	for(var i=0;i<objCardMe.arrCard.length;i++){
		if(objCardMe.arrCard[i].option == 1){
			count++;
			index = i;
			if(count > 1) break;
		}
	}
	if(count == 0 || count >1) return;
	else{
		objCardMe.click(index,0);
		var _round = round+1;
		
		var div = document.createElement('div');
		div.setAttribute('id','gamble1_'+_round+'');
		div.setAttribute('class','card_gamble');
		div.style.backgroundImage='url('+objCardMe.arrCard[index].suit[1]+')';
		document.getElementById("gamer1_gamble").appendChild(div);
		
		var span = document.createElement('span');
		span.setAttribute('id','spanGamble1_'+_round+'');
		span.setAttribute('class','spanNum');
		document.getElementById('gamble1_'+_round+'').appendChild(span);
		var txt = document.createTextNode(objCardMe.arrCard[index].num[1]);		
		document.getElementById('spanGamble1_'+_round+'').appendChild(txt);		
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[index].suit[2];		
		
		new Motion(div,[-50,130],[20.5*round,0],[35,100],[35,100],15);
		div.style.display ='block';
		div.style.zIndex = ''+_round+'';
		// Sau khi co function boc' hoac an thi round se duoc tang o day
		round++;
		
		removeElement('card1','card1_'+objCardMe.arrCard.length+'');
		// arrCardGamble[0].methodAdd(objCardMe.arrCard[index]);
		objCardMe.methodRemove(index);
		for(var i=index;i<objCardMe.arrCard.length;i++){
			var j = i+1;
			removeElement('card1','card1_'+j+'');
			var _div = document.createElement('div');
			_div.setAttribute('id','card1_'+j+'');
			_div.setAttribute('class','card');
			_div.setAttribute('onclick','clickCard(this)');
			_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			document.getElementById("card1").appendChild(_div);		
			var _span = document.createElement('span');
			_span.setAttribute('id','span'+j+'');
			_span.setAttribute('class','spanNum');
			_span.style.backgroundColor = '#fff';
			_span.style.color = objCardMe.arrCard[i].suit[2];
			document.getElementById('card1_'+j+'').appendChild(_span);
			var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
			document.getElementById('span'+j+'').appendChild(_txt);
			new Motion(_div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],20+i*5);
			_div.style.display = 'block';
			_div.style.zIndex = ''+j+'';
		}
	}
}

// Function chuyển hạ
function changeGamble(){

}


/******************** HA BAI ***********************/
var arrCardSV = new Array();
var arrCardSH = new Array();
var arrCardState = new Array();
//Kiem tra so fom ngang co the co, return mang fom ngang
function checkStateHor(){
	var _arrCardSH = new Array();
	var state = 0;
	for(var i=0;i<objCardMe.arrCard.length-2;i++){
		if(objCardMe.arrCard[i].checkH == true) continue;
		for(var j=i+1;j<objCardMe.arrCard.length-1;j++){
			if(objCardMe.arrCard[j].checkH == true) continue;
			for(var k=j+1;k<objCardMe.arrCard.length;k++){
				if(objCardMe.arrCard[k].checkH == true) continue;
				if(state==3) break;
				if(_arrCardSH.length==3) break;
				if(parseInt(objCardMe.arrCard[j].num[0]) == parseInt(objCardMe.arrCard[i].num[0]) &&
						parseInt(objCardMe.arrCard[k].num[0]) == parseInt(objCardMe.arrCard[j].num[0])){
					state++;
					objCardMe.setCheckH(i, true);objCardMe.setCheckH(j, true);objCardMe.setCheckH(k, true);
					_arrCardSH.push(new objCardState(state, [objCardMe.arrCard[i], objCardMe.arrCard[j], objCardMe.arrCard[k]]));
					for(var l=k+1;l<objCardMe.arrCard.length;l++){
						if(parseInt(objCardMe.arrCard[l].num[0]) == parseInt(objCardMe.arrCard[k].num[0])){
							for(var m=0;m<_arrCardSH.length;m++){
								if(_arrCardSH[m].state == state){
									objCardMe.setCheckH(l, true);
									_arrCardSH[m].methodAdd(objCardMe.arrCard[l]);
									break;
								}
							}
						}
					}
				}
			}			
		}
	}
	return _arrCardSH;
}
//Kiem tra so fom doc co the co, return mang fom doc
function checkStateVer(){
	if(objCardMe.flagSort == null) sortCard();
	var _arrCardSV = new Array();
	var state = 0;
	for(var i=0;i<objCardMe.arrCard.length-2;i++){
		if(objCardMe.arrCard[i].checkV == true) continue;
		for(var j=i+1;j<objCardMe.arrCard.length-1;j++){
			if(objCardMe.arrCard[j].checkV == true) continue;
			for(var k=j+1;k<objCardMe.arrCard.length;k++){
				if(objCardMe.arrCard[k].checkV == true) continue;
				if(state==3) break;
				if(_arrCardSV.length==3) break;
				if(parseInt(objCardMe.arrCard[j].suit[0]) == parseInt(objCardMe.arrCard[i].suit[0]) &&
						parseInt(objCardMe.arrCard[k].suit[0]) == parseInt(objCardMe.arrCard[j].suit[0])){
					var a = objCardMe.arrCard[i].num[0];
					var b = objCardMe.arrCard[j].num[0];
					var c = objCardMe.arrCard[k].num[0];
					if(a==b-1 && b==c-1){
						state++;
						objCardMe.setCheckV(i, true);objCardMe.setCheckV(j, true);objCardMe.setCheckV(k, true);
						_arrCardSV.push(new objCardState(state, [objCardMe.arrCard[i], objCardMe.arrCard[j], objCardMe.arrCard[k]]));
						for(var l=k+1;l<objCardMe.arrCard.length;l++){
							if(parseInt(objCardMe.arrCard[l].suit[0]) == parseInt(objCardMe.arrCard[k].suit[0])){
								if(c==objCardMe.arrCard[l].num[0]-1){
									for(var m=0;m<_arrCardSV.length;m++){
										if(_arrCardSV[m].state == state){
											objCardMe.setCheckV(l, true);
											c = objCardMe.arrCard[l].num[0];
											_arrCardSV[m].methodAdd(objCardMe.arrCard[l]);
										}
									}
								}
							}
						}
					}
				}
			}			
		}		
	}
	return _arrCardSV;
}

function clickDownCard(){
	var check = autoSetState();
	if(check == -1){
		alert("Ăn láo!!!");
	}else if(check == 0){
		alert("Móm!!");
	}else if(check >0){
		displayDownCard();
		if(check==10) alert("Ù.....");
		else if(check==100) alert("Ù đền.......");
	}
	
}

// Function hạ bài cua Me
function autoSetState(){
	for(var i=0;i<objCardMe.arrCard.length;i++){
		objCardMe.setCheckH(i, false);
		objCardMe.setCheckV(i, false);
	}
	arrCardSV = checkStateVer();
	arrCardSH = checkStateHor();
	
//	tempH/tempV: [][][]: [0]-phan tu i cua arrCardSH/arrCardSV, [1]-quan bai, [2]-phan tu j cua quan bai trong fom
	var tempH = new Array();
	var tempV = new Array();
//	tim nhung quan bai vua xep dc fom doc lan fom ngang trong arrCardSH, luu vao tempH
	for(var i=0;i<arrCardSH.length;i++){
		for(var j=0;j<arrCardSH[i].arrCard.length;j++){
			if(arrCardSH[i].arrCard[j].checkH == true && arrCardSH[i].arrCard[j].checkV == true){
				tempH.push([i, arrCardSH[i].arrCard[j], j]);
			}
		}
	}
//	tim nhung quan bai vua xep dc fom doc lan fom ngang trong arrCardSV, luu vao tempV
	for(var i=0;i<arrCardSV.length;i++){
		for(var j=0;j<arrCardSV[i].arrCard.length;j++){
			if(arrCardSV[i].arrCard[j].checkH == true && arrCardSV[i].arrCard[j].checkV == true){
				tempV.push([i, arrCardSV[i].arrCard[j], j]);
			}
		}
	}
	
	var tempCon = new Array();
	//[0]:quan bai,[1]:fom ngang thu i,[2]:card j ngang i,[3]:fom doc thu k,[4]:card m fom doc k
	for(var i=0;i<tempH.length;i++){
		for(var j=0;j<tempV.length;j++){
			if(tempH[i][1] == tempV[j][1]){
				tempCon.push([tempH[i][1], tempH[i][0], tempH[i][2], tempV[j][0], tempV[j][2]]);
				break;
			}
		}
	}	
	
	objCardState.prototype.priority = 0;
	//chia lai fom theo tempCon
	for(var i=0;i<tempCon.length;i++){
		if(arrCardSH[tempCon[i][1]].findV()>1 && arrCardSV[tempCon[i][3]].findH()==1){
			if(arrCardSH[tempCon[i][1]].findTake()==0){
				for(var j=0;j<tempCon.length;j++){
					if(tempCon[j][1] == tempCon[i][1]){
						arrCardSV[tempCon[j][3]].priority = 1;
					}
				}
				arrCardSH[tempCon[i][1]].priority = 0;
			}else if(arrCardSH[tempCon[i][1]].findTake()==1){
				var check = false;
				for(var j=0;j<tempCon.length;j++){
					if(tempCon[j][1]==tempCon[i][1] && tempCon[j][0].take==true){
						check = true;
						break;
					}
				}
				if(check==true){
					for(var j=0;j<tempCon.length;j++){
						if(tempCon[j][1] == tempCon[i][1]){
							arrCardSV[tempCon[j][3]].priority = 1;
						}
					}
					arrCardSH[tempCon[i][1]].priority = 0;
				}else{
					for(var j=0;j<tempCon.length;j++){
						if(tempCon[j][1] == tempCon[i][1]){
							arrCardSV[tempCon[j][3]].priority = 0;
						}
					}
					arrCardSH[tempCon[i][1]].priority = 1;
				}
			}else if(arrCardSH[tempCon[i][1]].findTake()>1){
				if(arrCardSH[tempCon[i][1]].arrCard.length==3){
					for(var j=0;j<tempCon.length;j++){
						if(tempCon[j][1] == tempCon[i][1]){
							arrCardSV[tempCon[j][3]].priority = 1;
						}
					}
					arrCardSH[tempCon[i][1]].priority = 0;
				}else if(arrCardSH[tempCon[i][1]].arrCard.length==4){
					var check =0;var state=-1;
					for(var j=0;j<tempCon.length;j++){
						if(tempCon[j][1]==tempCon[i][1] && tempCon[j][0].take==true){
							check++;state=j;
						}
					}
					if(check>1){//an it nhat 2 card vua xep doc va ngag
						for(var j=0;j<tempCon.length;j++){
							if(tempCon[j][1] == tempCon[i][1]){
								arrCardSV[tempCon[j][3]].priority = 1;
							}
						}
						arrCardSH[tempCon[i][1]].priority = 0;
					}else if(check==1){//an 1 card xep doc va ngang
						arrCardSV[tempCon[state][3]].priority = 1;
						arrCardSH[tempCon[i][1]].methodRemove(tempCon[i][2]);
						arrCardSH[tempCon[i][1]].priority = 1;
					}else if(check==0){//an lao'
						return -1;
					}
				}
			}
		}else if(arrCardSV[tempCon[i][3]].findH()>1 && arrCardSH[tempCon[i][1]].findV()==1){
			
		}
	}
	
	arrCardState = new Array();
	for(var i=0;i<arrCardSH.length;i++){
		if(tempCon.length==0){
			arrCardState.push(arrCardSH[i].arrCard);
		}else if(arrCardSH[i].priority == 1)
			arrCardState.push(arrCardSH[i].arrCard);
	}
	for(var i=0;i<arrCardSV.length;i++){
		if(tempCon.length==0){
			arrCardState.push(arrCardSV[i].arrCard);
		}else if(arrCardSV[i].priority == 1)
			arrCardState.push(arrCardSV[i].arrCard);
	}
	
	//Check xem fom doc co chia dc lam 2,3 fom rieng ko
	for(var i=0;i<arrCardSV.length;i++){
		if(arrCardSV.length == 3) break;
		if(arrCardSV[i].arrCard.length >=6 && arrCardSV[i].arrCard.length<9){
			var arrTemp = arrCardSV[i].arrCard.splice(3,arrCardSV[i].arrCard.length-1);
			var state = arrCardSV.length+1;
			arrCardSV.push(new objCardState(state, arrTemp));
		};
	}
	//dem so cay an
	var countTake = 0;
	var flagTake = true;
	for(var i=0;i<arrCardSH.length;i++){
		countTake = countTake+arrCardSH[i].findTake();
		if(arrCardSH[i].findTake() >= 2) flagTake = false;
	}
	for(var i=0;i<arrCardSV.length;i++){
		countTake = countTake+arrCardSV[i].findTake();
		if(arrCardSV[i].findTake() >= 2) flagTake = false;
	}
	
	var totalFom = arrCardState.length;
	//kiem tra xem co bao nhieu fom
	if(countTake != objCardMe.findTake() || totalFom < objCardMe.findTake() || flagTake==false){
		return -1;//AN LAO
	}else if(totalFom == 0){
		return 0;
	}else if(totalFom == 3){
		if(objCardMe.findTake()==3) return 100;
		else return 10;
	}else if(totalFom != 0){
		return 1;
	}
}

// Hien thi ha fom cua Me
function displayDownCard(){
	for(var i=1;i<=objCardMe.arrCard.length;i++){
		removeElement('card1','card1_'+i+'');
	}
	//	Xoa bo cac thanh phan trong objCardMe va hien thi
	for(var j=0; j<arrCardState.length;j++){
		for(var k=0;k<arrCardState[j].length;k++){
			for(var i=0;i<objCardMe.arrCard.length;i++){
				if(objCardMe.arrCard[i].num[0] == arrCardState[j][k].num[0] && 
						objCardMe.arrCard[i].suit[0] == arrCardState[j][k].suit[0]){
				objCardMe.methodRemove(i);
				break;
				}
			}
		}
	}
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		var _div = document.createElement('div');
		_div.setAttribute('id','card1_'+j+'');
		_div.setAttribute('class','card');
		_div.setAttribute('onclick','clickCard(this)');
		_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
		document.getElementById("card1").appendChild(_div);		
		var _span = document.createElement('span');
		_span.setAttribute('id','span'+j+'');
		_span.setAttribute('class','spanNum');
		_span.style.backgroundColor = '#fff';
		_span.style.color = objCardMe.arrCard[i].suit[2];
		document.getElementById('card1_'+j+'').appendChild(_span);
		var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		document.getElementById('span'+j+'').appendChild(_txt);
		new Motion(_div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],20+i*5);
		_div.style.display = 'block';
		_div.style.zIndex = ''+j+'';
	}
	
	//	Hien thi ra cho ha bai
	document.getElementById('down1').style.display = 'block';
	for(var i=0; i<arrCardState.length;i++){
		var a = i+1;
		var div = document.createElement('div');
		div.setAttribute('id','down1_state'+a+'');
		div.setAttribute('class','downState1');
		div.style.display = 'block';
		var top = -25*i;
		div.style.top = ''+top+'%';
		div.style.zIndex = ''+a+'';
		document.getElementById("down1").appendChild(div);
		for(var j=0;j<arrCardState[i].length;j++){
			var b =j+1;
			var _div = document.createElement('div');
			_div.setAttribute('id', 'down1_state'+a+b);
			_div.setAttribute('class', 'card');
			_div.style.backgroundImage='url('+arrCardState[i][j].suit[1]+')';
			_div.style.display = 'block';
			_div.style.zIndex = ''+b+'';
			div.appendChild(_div);
			var span = document.createElement('span');
			span.setAttribute('id','span1_state'+a+b);
			span.setAttribute('class','spanNum');
			span.style.backgroundColor = '#fff';
			span.style.color = arrCardState[i][j].suit[2];
			var txt = document.createTextNode(arrCardState[i][j].num[1]);
			span.appendChild(txt);
			_div.appendChild(span);
			new Motion(_div,[0,0],[17*j,0],[26,99],[26,99],100);
		}
	}
}

// Function lay thong tin hạ bài cua cac gamer # tu server va hien thi
function getDownCard(){
 
}


/******************** BOC BAI ***********************/
// Function click boc bai cua Me
function clickAddCard(){
	setAddCard();
}

// Function bốc bài
function setAddCard(){
	// Add vao objCardMe quan bai duoc gui tu server ve
	objCardMe.methodAdd(new objCard(1,4));
	// Hien thi bai an ve bai cua minh
	var _div = document.createElement('div');
	_div.setAttribute('id','card1_10');
	_div.setAttribute('class','card');
	_div.setAttribute('onclick','clickCard(this)');
	_div.style.backgroundImage='url('+objCardMe.arrCard[9].suit[1]+')';
	document.getElementById("card1").appendChild(_div);		
	var _span = document.createElement('span');
	_span.setAttribute('id','span10');
	_span.setAttribute('class','spanNum');
	_span.style.backgroundColor = '#fff';
	_span.style.color = objCardMe.arrCard[9].suit[2];
	document.getElementById('card1_10').appendChild(_span);
	var _txt = document.createTextNode(objCardMe.arrCard[9].num[1]);
	document.getElementById('span10').appendChild(_txt);
	new Motion(_div,[50,-225],[9.5*9,0],[0,0],[12.5,100],100);
	_div.style.display = 'block';
	_div.style.zIndex = '10';
}


/******************** AN BAI ***********************/
// Function click de an bai cua Me
function clickTakeCard(){
	setTakeCard();
}

// Function ăn bài cua Me
function setTakeCard(){
	var _round = round+1;
	// Hien thi remove bai o arrCardGamble[max];
	removeElement('gamer'+totalGamer+'_gamble','gamble'+totalGamer+'_'+_round+'');
	// remove obj arrCardGamble[max].arrCard[round] va add obj card vao objCardMe;
	objCardMe.methodAdd(arrCardGamble[totalGamer-1].arrCard[round]);
	objCardMe.setTake(objCardMe.arrCard.length-1);
	arrCardGamble[totalGamer-1].methodRemove(round);
	// Hien thi bai an ve bai cua minh
	var _div = document.createElement('div');
	_div.setAttribute('id','card1_10');
	_div.setAttribute('class','card');
	_div.setAttribute('onclick','clickCard(this)');
	_div.style.backgroundImage='url('+objCardMe.arrCard[9].suit[1]+')';
	document.getElementById("card1").appendChild(_div);		
	var _span = document.createElement('span');
	_span.setAttribute('id','span10');
	_span.setAttribute('class','spanNum');
	_span.style.backgroundColor = '#fff';
	_span.style.color = objCardMe.arrCard[9].suit[2];
	document.getElementById('card1_10').appendChild(_span);
	var _txt = document.createTextNode(objCardMe.arrCard[9].num[1]);
	document.getElementById('span10').appendChild(_txt);
	new Motion(_div,[24,-22],[9.5*9,0],[12.5,100],[12.5,100],100);
	_div.style.display = 'block';
	_div.style.zIndex = '10';
}

// Function lay thong tin an bai cua thag # dc gui tu server
function getTakeCard(){

}


/******************** GUI BAI ***********************/
// Function gửi bài
function attachCard(){
 
}





